package cn.iocoder.yudao.module.worker.dal.mysql.reward;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.worker.controller.app.reward.vo.AppWorkerRewardPageReqVO;
import cn.iocoder.yudao.module.worker.dal.dataobject.reward.WorkerRewardDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.worker.controller.admin.reward.vo.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 打赏记录 Mapper
 *
 * @author Jerry
 */
@Mapper
public interface WorkerRewardMapper extends BaseMapperX<WorkerRewardDO> {

    default PageResult<WorkerRewardDO> selectPage(WorkerRewardPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WorkerRewardDO>()
                .eqIfPresent(WorkerRewardDO::getUserId, reqVO.getUserId())
                .eqIfPresent(WorkerRewardDO::getWorkerClerkId, reqVO.getWorkerClerkId())
                .eqIfPresent(WorkerRewardDO::getWorkerUserId, reqVO.getWorkerUserId())
                .eqIfPresent(WorkerRewardDO::getRewardType, reqVO.getRewardType())
                .eqIfPresent(WorkerRewardDO::getGiftId, reqVO.getGiftId())
                .likeIfPresent(WorkerRewardDO::getGift, reqVO.getGift())
                .eqIfPresent(WorkerRewardDO::getCount, reqVO.getCount())
                .eqIfPresent(WorkerRewardDO::getPayMoney, reqVO.getPayMoney())
                .eqIfPresent(WorkerRewardDO::getFee, reqVO.getFee())
                .eqIfPresent(WorkerRewardDO::getMoney, reqVO.getMoney())
                .eqIfPresent(WorkerRewardDO::getWeixin, reqVO.getWeixin())
                .eqIfPresent(WorkerRewardDO::getMsg, reqVO.getMsg())
                .eqIfPresent(WorkerRewardDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(WorkerRewardDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(WorkerRewardDO::getId));
    }

    default PageResult<WorkerRewardDO> selectPage(AppWorkerRewardPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WorkerRewardDO>()
                .eqIfPresent(WorkerRewardDO::getUserId, reqVO.getUserId())
                .eqIfPresent(WorkerRewardDO::getWorkerClerkId, reqVO.getWorkerClerkId())
                .eqIfPresent(WorkerRewardDO::getWorkerUserId, reqVO.getWorkerUserId())
                .eqIfPresent(WorkerRewardDO::getRewardType, reqVO.getRewardType())
                .eqIfPresent(WorkerRewardDO::getGiftId, reqVO.getGiftId())
                .eqIfPresent(WorkerRewardDO::getGift, reqVO.getGift())
                .eqIfPresent(WorkerRewardDO::getCount, reqVO.getCount())
                .eqIfPresent(WorkerRewardDO::getPayMoney, reqVO.getPayMoney())
                .eqIfPresent(WorkerRewardDO::getFee, reqVO.getFee())
                .eqIfPresent(WorkerRewardDO::getMoney, reqVO.getMoney())
                .eqIfPresent(WorkerRewardDO::getWeixin, reqVO.getWeixin())
                .eqIfPresent(WorkerRewardDO::getMsg, reqVO.getMsg())
                .eq(WorkerRewardDO::getStatus, true)
                .betweenIfPresent(WorkerRewardDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(WorkerRewardDO::getId));
    }

    default Integer getSumPrice(Long clerkId){
        Integer sumPrice = 0;
        Integer sum = selectJoinOne(Integer.class, new MPJLambdaWrapperX<WorkerRewardDO>()
                .selectSum(WorkerRewardDO::getMoney)
                .eq(WorkerRewardDO::getStatus, true)
                .eq(WorkerRewardDO::getWorkerClerkId, clerkId));
        if(sum != null){
            sumPrice = sum;
        }
        return sumPrice;
    }

    default Integer getWeekSumPrice(Long clerkId, LocalDateTime time){
        Integer weekSum = 0;
        Integer sum = selectJoinOne(Integer.class, new MPJLambdaWrapperX<WorkerRewardDO>()
                .selectSum(WorkerRewardDO::getMoney)
                .eq(WorkerRewardDO::getStatus, true)
                .gt(WorkerRewardDO::getCreateTime, time)
                .eq(WorkerRewardDO::getWorkerClerkId, clerkId));
        if(sum != null){
            weekSum = sum;
        }
        return weekSum;
    }

    default List<WorkerRewardDO> getTopList(){
        return selectList(new LambdaQueryWrapperX<WorkerRewardDO>()
                .eq(WorkerRewardDO::getStatus, true)
                .orderByDesc(WorkerRewardDO::getCreateTime).last("LIMIT 10"));
    }

    default List<WorkerRewardDO> getGiftList(Long userId, Long workerClerkId){
        return selectList(new LambdaQueryWrapperX<WorkerRewardDO>()
                .eqIfPresent(WorkerRewardDO::getWorkerUserId, userId)
                .eqIfPresent(WorkerRewardDO::getWorkerClerkId, workerClerkId)
                .eq(WorkerRewardDO::getStatus, true));
    }
}